package com.uusee.crawler.job;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.uusee.crawler.dbwriter.UgcDbWriter;
import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.framework.AbstractJob;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.video.PlayPageProcessor;
import com.uusee.crawler.xmlwriter.UgcXmlWriter;
import com.uusee.shipshape.sp.model.Ugc;

public class TudouUgcFastCrawlJob  extends AbstractJob {
	private FetchHTTP fetch = new FetchHTTP();
	private PlayPageProcessor pageProcessor = new PlayPageProcessor();
	private UgcXmlWriter xmlwriter = new UgcXmlWriter();
	private UgcDbWriter dbWriter = new UgcDbWriter();
	
	public static void main(String[] args) {
		try {
			ApplicationContext acx = new ClassPathXmlApplicationContext("applicationContext-ugc.xml");
			TudouUgcFastCrawlJob tudouUgcFastCrawlJob = (TudouUgcFastCrawlJob)acx.getBean("tudouUgcFastCrawlJob");
			tudouUgcFastCrawlJob.doExecute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public CrawlURI createCrawlURI(String vid) {
		String playUrl = "http://www.tudou.com/programs/view/"+vid+"/";
		Ugc ugc = new Ugc();
		ugc.setVid(vid+"");
		ugc.setVideoSite("www.tudou.com");
		ugc.setPlayUrl(playUrl);
		
		CrawlURI crawlURI = new CrawlURI();
		crawlURI.setSourceSite("www.tudou.com");
		crawlURI.setCrawlUrl(playUrl);
		crawlURI.setModel(ugc);
		crawlURI.setCharset("gbk");
		
		return crawlURI;
	}
	
	public void crawl(CrawlURI crawURI) {
		try {
				fetch.process(crawURI);
				pageProcessor.process(crawURI);
				//xmlwriter.process(crawURI);
				dbWriter.process(crawURI);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void setFetch(FetchHTTP fetch) {
		this.fetch = fetch;
	}

	public void setPageProcessor(PlayPageProcessor pageProcessor) {
		this.pageProcessor = pageProcessor;
	}

	public void setXmlwriter(UgcXmlWriter xmlwriter) {
		this.xmlwriter = xmlwriter;
	}

	public void setDbWriter(UgcDbWriter dbWriter) {
		this.dbWriter = dbWriter;
	}
}
